/**
 * Mise en oeuvre de Piece.h
 *
 * @file Piece.cxx
 */

// A besoin de la declaration de la classe
#include "Piece.h"
#include "Echiquier.h"

#include <iostream>

using namespace std;

Piece::Piece()
{
	// ne fait rien => objet instancie mais non valide.
	//cout << "Piece() - " << this << endl;
}

Piece::Piece(int x, int y, bool color)
	: m_x(x), m_y(y), m_white(color)
{
	//cout << "Piece(int x, int y, bool color) - " << this << endl;
}

Piece::Piece(const Piece &p)
	: m_x(p.x()), m_y(p.y()), m_white(p.isWhite())
{
	//cout << "Piece(const Piece &p) - " << this << endl;
}

Piece::~Piece()
{
	//cout << "~Piece() - " << this << endl;
}

void Piece::init(int x, int y, bool white)
{
	m_x = x;
	m_y = y;
	m_white = white;
}

void Piece::move(int x, int y)
{
	m_x = x;
	m_y = y;
}

int Piece::x() const
{
	return m_x;
}

int Piece::y() const
{
	return m_y;
}

bool Piece::isWhite() const
{
	return m_white;
}

bool Piece::isBlack() const
{
	return ! this->isWhite();
}

void Piece::affiche() const
{
	cout << "Piece: x=" << this->x() << " y=" << this->y() << " " << (this->isBlack() ? "noire" : "blanche") << endl;
}

bool Piece::mouvementValide(Echiquier *e, int x, int y)
{
	return true;
}

bool Piece::mouvementAutorise(Echiquier *e, int x, int y)
{
	if (x < 1 || x > 8 || y < 1 || y > 8)
		return false;

	PiecePtr piece = e->getPiece(x, y);

	if (piece)
		return this->isWhite() && piece->isBlack() || this->isBlack() && piece->isWhite();

	return true;
}
